#include "devicelist.h"
#include <QDebug>
using namespace Moolink;

DeviceList::DeviceList(QObject *parent) :
    QAbstractListModel(parent)
{
    device_list.clear();
    m_roleNames.insert(RoleName, "name");
    m_roleNames.insert(RolePortname, "portName");


    m_roleNames.insert(RoleProtocolId, "protocolId");
    m_roleNames.insert(RoleProtocolLib, "protocolLib");
    m_roleNames.insert(RoleProtocol, "protocol");
    m_roleNames.insert(RoleCommaddr, "commAddr");

//    m_roleNames.insert(RoleHJcode, "code");
//    m_roleNames.insert(RoleHJcodeName, "codeName");
//    m_roleNames.insert(RoleHJunit, "unit");
//    m_roleNames.insert(RoleHJunitTotal, "unitTotal");

    m_roleNames.insert(RolecolorCode, "colorCode");
    m_roleNames.insert(RoleSystemTime, "systemTime");
    m_roleNames.insert(RoledataString, "dataString");


    m_roleNames.insert(RoleDeviceId, "deviceId");





    //从数据库初始化设备参数
//    device_list.push_back(Device("sensor01","COM10","A",1));
//    device_list.push_back(Device("sensor02","COM10","A",2));
//    device_list.push_back(Device("sensor03","COM10","A",3));
//    device_list.push_back(Device("sensor04","COM10","A",4));
//    device_list.push_back(Device("sensor05","COM10","A",5));
//    device_list.push_back(Device("sensor06","COM10","A",6));
//    device_list.push_back(Device("sensor07","COM10","A",7));

}


int DeviceList::rowCount(const QModelIndex &parent) const
{
    return device_list.size();
}

int DeviceList::rowCountRead()
{
    return device_list.size();
}


QVariant DeviceList::data(const QModelIndex &index, int role) const
{
    Device ele = device_list.at(index.row());
    QVariant var;
    switch (role) {

    case RoleName:
        var = ele.name;
        break;
    case RolePortname:
        var = ele.portName;
        break;
    case RoleProtocolLib:
        var = ele.protocolLib;
        break;
    case RoleProtocolId:
        var = ele.protocolId;
        break;
    case RoleProtocol:
        var = ele.protocol;
        break;
    case RoleCommaddr:
        var = ele.comAddr;
        break;
//    case RoleHJcode:
//        var = ele.code;
//        break;
//    case RoleHJcodeName:
//        var = ele.codeName;
//        break;
//    case RoleHJunit:
//        var = ele.unit;
//        break;
//    case RoleHJunitTotal:
//        var = ele.unitTotal;
//        break;
    case RolecolorCode:
        var = ele.colorCode;
        break;
    case RoleSystemTime:
        var = ele.SystemTime;
        break;
    case RoledataString:
        var = ele.dataString;
        break;
    case RoleDeviceId:
        var = ele.id;
        break;

    default:
        break;
    }

    return var;
}


QHash<int, QByteArray> DeviceList::roleNames() const
{
    return m_roleNames;
}

void DeviceList::changeName(int index)
{
    QString timeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    beginResetModel();
    device_list[index].dataString="瞬时流量：1000 m3 , 累积流量5000 m3   @ "+timeStr;
    endResetModel();

//    if(index == 1){
//        Device ele1("change01","COM10","A",1);
//        device_list[index] = ele1;
//        qDebug()<<"changeName(1)"<<device_list[index].name;
//    }

//    if(index == 2){
//        Device ele2("change02","COM10","A",1);
//        device_list[index] = ele2;
//        qDebug()<<"changeName(2)"<<device_list[index].name;
//    }

    //emit dataChanged();
}

void DeviceList::clickItem(int index)
{
    //取出设备端口，取出设备协议，取工作任务

    //打开设备端口，根据工作任务调用协议数据生成方法指令数据，从端口发送指令

    //等待取回端口数据，根据工作任务调用协议数据分析方法返回所需要的数据

    //更新界面数据显示字串
    QString timeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    beginResetModel();
    device_list[index].dataString="瞬时流量：1000 m3 , 累积流量5000 m3   @ "+timeStr;
    endResetModel();
}

void DeviceList::changeDataString(int index,QString str,int error)
{
    //取出设备端口，取出设备协议，取工作任务

    //打开设备端口，根据工作任务调用协议数据生成方法指令数据，从端口发送指令

    //等待取回端口数据，根据工作任务调用协议数据分析方法返回所需要的数据

    //更新界面数据显示字串
    beginResetModel();
    if(0==error) device_list[index].colorCode="green"; else device_list[index].colorCode="red";
    device_list[index].dataString=str;
    endResetModel();
}


//void DeviceList::addDevice(QString name_, QString portname_, QString protocollib_, QString protocol_, QString addr_)
//{
//    beginInsertRows(QModelIndex(), device_list.count(), device_list.count());
//    device_list.push_back(Device(name_,portname_,protocollib_,protocol_,addr_.toInt()));
//    endInsertRows();
//    qDebug()<<"addDevice"<<device_list.size();
//    //emit dataChanged();
//}
int DeviceList::getIndexByDeviceId(QString deviceId)
{
    int listsize=device_list.size();
    for(int i=0;i<listsize;i++)
    {
        if(device_list[i].id==deviceId)
        {
            return i;
        }
    }
    return -1;
}


void DeviceList::addDevice(QVariant devVar)
{
    beginInsertRows(QModelIndex(), device_list.count(), device_list.count());
    device_list.push_back(Device(devVar));
    //device_list.last().dataProcessFun=IndexGuide::getDataProcessFun(device_list.last().protocolLib);
    IndexGuide::setDataProcessFun(&device_list.last());
    IndexGuide::setCmdProcessFun(&device_list.last());
    endInsertRows();
    qDebug()<<"addDevice"<<device_list.size();
}
void DeviceList::updateDevice(QVariant devVar)
{
    int listsize=device_list.size();
    for(int i=0;i<listsize;i++)
    {
        if(device_list[i].id==devVar.value<QVariantMap>()["deviceId"].toString())
        {
            beginResetModel();
            device_list[i].UpdateDevice(devVar);
            IndexGuide::setDataProcessFun(&device_list[i]);
            IndexGuide::setCmdProcessFun(&device_list[i]);
            endResetModel();
            break;
        }
    }
}
